package net.lab1024.sa.admin.module.business.erp.mmo.service;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import net.lab1024.sa.admin.module.business.erp.mmo.dao.MrnordDao;
import net.lab1024.sa.admin.module.business.erp.mmo.dao.MrnordaDao;
import net.lab1024.sa.admin.module.business.erp.mmo.dao.MrqordaDao;
import net.lab1024.sa.admin.module.business.erp.mmo.domain.entity.MrqordaEntity;
import net.lab1024.sa.admin.module.business.erp.mmo.domain.form.MrnordAddForm;
import net.lab1024.sa.admin.module.business.erp.mmo.domain.form.MrnordQueryForm;
import net.lab1024.sa.admin.module.business.erp.mmo.domain.form.MrnordUpdateForm;
import net.lab1024.sa.admin.module.business.erp.mmo.domain.vo.MrnordVO;
import net.lab1024.sa.admin.module.business.erp.mmo.domain.vo.MrnordaVO;
import net.lab1024.sa.admin.module.business.erp.mmo.domain.entity.MrnordEntity;
import net.lab1024.sa.admin.module.business.erp.mmo.domain.entity.MrnordaEntity;
import net.lab1024.sa.base.common.code.ErpErrorCode;
import net.lab1024.sa.base.common.util.SmartBeanUtil;
import net.lab1024.sa.base.common.util.SmartPageUtil;
import net.lab1024.sa.base.common.domain.ResponseDTO;
import net.lab1024.sa.base.common.domain.PageResult;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 领料通知单 Service
 *
 * @Author AKITA
 * @Date 2025-08-17 17:01:16
 * @Copyright AKITA
 */

@Service
public class MrnordService {

    @Resource
    private MrnordDao mrnordDao;
    @Resource
    private MrnordaDao mrnordaDao;
    @Autowired
    private MrqordaDao mrqordaDao;


    public PageResult<MrnordVO> queryPage(MrnordQueryForm queryForm) {
        Page<?> page = SmartPageUtil.convert2PageQuery(queryForm);
        List<MrnordVO> list = mrnordDao.queryPage(page, queryForm);
        return SmartPageUtil.convert2PageResult(page, list);
    }

    @Transactional
    public ResponseDTO<Long> add(MrnordAddForm addForm) {
        MrnordEntity mrnordEntity = SmartBeanUtil.copy(addForm, MrnordEntity.class);
        mrnordDao.insert(mrnordEntity);
        for (MrnordaEntity mrnordaEntity : addForm.getLista()) {
            mrnordaEntity.setId(mrnordEntity.getId());
            mrnordaDao.insert(mrnordaEntity);
        }
        return ResponseDTO.ok(mrnordEntity.getId());
    }


    public ResponseDTO<String> update(MrnordUpdateForm updateForm) {
        MrnordEntity mrnordEntity = SmartBeanUtil.copy(updateForm, MrnordEntity.class);
        mrnordDao.updateById(mrnordEntity);
        return ResponseDTO.ok();
    }


    public ResponseDTO<String> batchDelete(List<Long> idList) {
        if (CollectionUtils.isEmpty(idList)){
            return ResponseDTO.ok();
        }

        mrnordDao.deleteBatchIds(idList);
        return ResponseDTO.ok();
    }


    @Transactional
    public ResponseDTO<String> delete(Long id) {


        List<MrqordaEntity> entitiesa = mrqordaDao.selectList(new LambdaQueryWrapper<MrqordaEntity>().eq(MrqordaEntity::getBaseId, id));

        if (!entitiesa.isEmpty()){
            return ResponseDTO.error(ErpErrorCode.PARAM_ERROR,"存在生产领料单，禁止删除！");
        }



        mrnordDao.deleteById(id);
        mrnordaDao.deleteById(id);
        return ResponseDTO.ok();
    }


    public ResponseDTO<MrnordVO> info(Long id) {
        MrnordEntity mrnordEntity = mrnordDao.selectById(id);
        MrnordVO copy = SmartBeanUtil.copy(mrnordEntity, MrnordVO.class);
        List<MrnordaEntity> mrnordaEntities = mrnordaDao.selectList(new LambdaQueryWrapper<MrnordaEntity>().eq(MrnordaEntity::getId, id));
        List<MrnordaVO> mrnordaVOS = SmartBeanUtil.copyList(mrnordaEntities, MrnordaVO.class);
        copy.setLista(mrnordaVOS);
        return ResponseDTO.ok(copy);


    }

    public ResponseDTO<MrnordVO> infoUnexecute(Long id) {
        List<MrnordVO> poordVOS = mrnordDao.infoUnexecute(id);

        return ResponseDTO.ok(poordVOS.get(0));
    }
}
